import { toRaw } from 'vue'

const { ipcRenderer } = require('electron')

export default {
  state: {
    systemPrintList: [], // 系统打印机列表
    systemPrintTask: {}, // 系统打印机任务列表
  },
  getters: {
    realTimeSystemPrintTask: (state) => {
      return state.systemPrintTask
    },
  },
  mutations: {
    SET_SYSTEMPRINTLIST(state, list) {
      state.systemPrintList = list
    },
    SET_SYSTEMPRINTTASK(state, data) {
      let task = state.systemPrintTask
      switch (data.type) {
        case 'push': {
          if (!task[data.name]) {
            task[data.name] = []
          }
          task[data.name].push(data.item)
          break
        }
        case 'delete': {
          if (task[data.name] && task[data.name].length) {
            task[data.name] = task[data.name].filter((f) => f.printId !== data.item.printId)
          }
          break
        }
        case 'update': {
          if (task[data.name] && task[data.name].length) {
            const taskItem = task[data.name].find((f) => f.printId === data.item.printId)
            taskItem && Object.assign(taskItem, data.item)
          }
          break
        }
        case 'deleteAll': {
          task = {}
          break
        }
      }
      state.systemPrintTask = task
    },
  },
  actions: {
    async getPrintList({ commit }, data) {
      const list = await ipcRenderer.invoke('getPrintList', data)
      list.forEach((l) => {
        l.isActive = l.isDefault
      })
      console.log('list', list)
      commit('SET_SYSTEMPRINTLIST', list)
    },
  },
}
